package aqr;

public class StringUtils {

	public static void main(String[] args) {
		
		String test = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//		System.out.println("Test before: " + test);
		
//		System.out.println("Test after: "+ replace(test, 'B'));
//		System.out.println("Test after: "+ replace(test, new String("ABD").toCharArray()));
//		System.out.println("Reverse string: "+reverse(test));
//		System.out.println("Reverse string: "+reverseRec(test));
	}

	// Function to replace a given character from a string
	static String replace(String str, char chr) {
		// return str.replaceAll(String.valueOf(chr), "");
		String rep = "";
		for (char c : str.toCharArray()) {
			if (c != chr)
				rep += String.valueOf(c);
		}
		return rep;
	}

	// Function to replace a given set of characters from a string
	static String replace(String str, char[] chrs) {
		String rep = "", flag = "";
		for (char c : str.toCharArray()) {
			for (char x : chrs) {
				flag = String.valueOf(c);
				if (c == x) {
					flag = ""; 
					break;
				}
			}
			rep += flag;
		}
		return rep;
	}
	
	// Function to reverse a given string
	static String reverse(String str) {
		StringBuilder rev = new StringBuilder();
		for(int i=str.length()-1; i>=0; i--) {
			rev.append(str.charAt(i));
		}
		return rev.toString();
	}
	
	// Function to reverse a given string using recursion
	static String reverseRec(String str) {
		if(str.length() < 2) {
			return str;
		}
		return reverseRec(str.substring(1)) + str.charAt(0);
	}
}
